Passed
Pull Request — master (#112)
by Mathieu
01:59
created

DateUtilsAdapter.getEasterDate   A

Complexity

Conditions 1

Size

Total Lines 19
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 19
c 0
b 0
f 0
rs 9.45
cc 1
1
import {Injectable} from '@nestjs/common';
2
import {
3
  format as fnsFormat,
4
  isWeekend as fnsIsWeekend,
5
  getDaysInMonth as fnsGetDaysInMonth,
6
  eachDayOfInterval,
7
  addDays
8
} from 'date-fns';
9
import {IDateUtils} from 'src/Application/IDateUtils';
10
11
@Injectable()
12
export class DateUtilsAdapter implements IDateUtils {
13
  constructor(private readonly date: Date = new Date()) {}
14
15
  public format(date: Date, format: string): string {
16
    return fnsFormat(date, format);
17
  }
18
19
  public getDaysInMonth(date: Date): number {
20
    return fnsGetDaysInMonth(date);
21
  }
22
23
  public isWeekend(date: Date): boolean {
24
    return fnsIsWeekend(date);
25
  }
26
27
  public getCurrentDate(): Date {
28
    return this.date;
29
  }
30
31
  public getCurrentDateToISOString(): string {
32
    return this.date.toISOString();
33
  }
34
35
  public getWorkedDaysDuringAPeriod(start: Date, end: Date): Date[] {
36
    const dates: Date[] = [];
37
    const workedFreeDays: Date[] = [];
38
39
    for (let year = start.getFullYear(); year <= end.getFullYear(); year++) {
40
      workedFreeDays.push(...this.getWorkedFreeDays(year));
41
    }
42
43
    for (const day of eachDayOfInterval({start, end})) {
44
      if (
45
        this.isWeekend(day) ||
46
        workedFreeDays.filter(d => d.toISOString() === day.toISOString())
47
          .length > 0
48
      ) {
49
        continue;
50
      }
51
52
      dates.push(day);
53
    }
54
55
    return dates;
56
  }
57
58
  public getWorkedFreeDays(year: number): Date[] {
59
    const fixedDays: Date[] = [
60
      new Date(`${year}-01-01`), // New Year's Day
61
      new Date(`${year}-05-01`), // Labour Day
62
      new Date(`${year}-05-08`), // Victory in 1945
63
      new Date(`${year}-07-14`), // National Day
64
      new Date(`${year}-08-15`), // Assumption
65
      new Date(`${year}-11-01`), // All Saints' Day
66
      new Date(`${year}-11-11`), // The Armistice
67
      new Date(`${year}-12-25`) // Christmas
68
    ];
69
70
    const easterDate = this.getEasterDate(year);
71
    const easterDays: Date[] = [
72
      addDays(easterDate, 1), // Easter Monday
73
      addDays(easterDate, 39) // Ascension
74
    ];
75
76
    return [...fixedDays, ...easterDays];
77
  }
78
79
  public getEasterDate(year: number): Date {
80
    const a = year % 19;
81
    const b = Math.floor(year / 100);
82
    const c = year % 100;
83
    const d = Math.floor(b / 4);
84
    const e = b % 4;
85
    const f = Math.floor((b + 8) / 25);
86
    const g = Math.floor((b - f + 1) / 3);
87
    const h = (19 * a + b - d - g + 15) % 30;
88
    const i = Math.floor(c / 4);
89
    const k = c % 4;
90
    const l = (32 + 2 * e + 2 * i - h - k) % 7;
91
    const m = Math.floor((a + 11 * h + 22 * l) / 451);
92
    const n0 = h + l + 7 * m + 114;
93
    const n = Math.floor(n0 / 31) - 1;
94
    const p = (n0 % 31) + 1;
95
96
    return new Date(year, n, p);
97
  }
98
}
99